﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VatTuDTO;
using VatTuBUS1;

namespace CungUngXayDung.admin
{
    public partial class ChiTietLoaiSanPham : System.Web.UI.Page
    {
        LoaiSanPhamDTO lsp= new LoaiSanPhamDTO();
        protected void Page_Load(object sender, EventArgs e)
        {
              LoadDanhSachChungLoaiSP();
              string catid = Request.QueryString["catid"].ToString();
              int malsp = Convert.ToInt32(catid);
              if (malsp != 0)
              {
                  LayThongTinLoaiSanPham(malsp);
              }
        }
        //Lay THong TIn Loai san Pham theo MaLoaiSanPham
        void LayThongTinLoaiSanPham(int maloai)
        {
            lsp = new LoaiSanPhamBUS().SelectLoaiSanPhamTheoMaLoai(maloai);
            txtTenLoaiSanPham.Text = lsp.TenLoaiSanPham.ToString();
            ddlChungLoaiSanPham.DataValueField = lsp.MaChungLoai.ToString();
            Dispose();
        }

        //---------------------------------
        void LoadDanhSachChungLoaiSP()
        {
            List<ChungLoaiDTO> lst = new List<ChungLoaiDTO>();
            lst = new ChungLoaiBUS().SelectChungLoai();
            ddlChungLoaiSanPham.DataSource = lst;
            ddlChungLoaiSanPham.DataValueField = "MaChungLoai";
            ddlChungLoaiSanPham.DataTextField = "TenChungLoai";
            ddlChungLoaiSanPham.DataBind();
            Dispose();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = "QLLoaiSanPham.aspx";
            Response.Redirect(url);
        }

        //su ly cap nhat lai san pham
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string catid = Request.QueryString["catid"].ToString();
            int dk = Convert.ToInt32(catid);
            if (dk != 0)
            {
              
                lsp.TenLoaiSanPham = Request.Form[2].ToString();
                lsp.MaChungLoai = int.Parse(Request.Form[3].ToString());
             
                new LoaiSanPhamBUS().UpdateLoaiSanPham(lsp);
                string url = "QLLoaiSanPham.aspx";
                Response.Redirect(url);
            }
            if (dk == 0)
            {
                
                lsp.TenLoaiSanPham = Request.Form[2].ToString();
                lsp.MaChungLoai =int.Parse(Request.Form[3].ToString());
                
                Boolean n= new LoaiSanPhamBUS().InsertLoaiSanPham(lsp);
                string url = "QLLoaiSanPham.aspx";
                Response.Redirect(url);
            }
        }

      

    }
}